package edu.ctbu.labtest.service;

import edu.ctbu.labtest.dao.TeacherRepository;
import edu.ctbu.labtest.domain.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherService {

    @Autowired
    private TeacherRepository teacherRepository;

    /*读取所有老师的数据*/
    public List<Teacher> findAll() { return teacherRepository.findAll(); }

    /*插入老师*/
    public Teacher insert(Teacher teacher){
        return teacherRepository.save(teacher);
    }

    /*更新老师*/
    public Teacher update(Teacher teacher){
        return teacherRepository.save(teacher);
    }

    /*删除老师*/
    public void delete(Teacher teacher){
        teacherRepository.delete(teacher);
    }

    /*通过id查找老师*/
    public Teacher getById(Long id){
        Teacher teacher = teacherRepository.findById(id).orElse(null);
        return teacher;
    }

    /*通过姓名查找老师*/
    public List<Teacher> getByName(String name){
        return teacherRepository.findByName(name);
    }
}
